package com.fnst.caesar.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.*;

/**
 * Servlet Filter implementation class Security_Filter
 */
public class Security_Filter implements Filter
{

	/**
	 * Default constructor.
	 */
	public Security_Filter()
	{
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy()
	{
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException
	{
		// TODO Auto-generated method stub
		// place your code here
		HttpServletRequest req= (HttpServletRequest)request;
        HttpServletResponse res= (HttpServletResponse)response;
        HttpSession session = req.getSession();
        if(session == null)
        {
        	//request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
        	//return;
        }
        
        String user_id = (String)session.getAttribute("user_id");
        String user_role = (String)session.getAttribute("user_role");
        String request_uri=req.getRequestURI();
        String target_URL = request_uri.substring(request_uri.indexOf("/"),	request_uri.length());
        
        if(user_id == null || user_id.equals(""))
        {
        	//res.sendRedirect("login"); 
            //return;
        }
        else
        {
        	if(user_role.equals("ATPUser"))
        	{
        		if(target_URL.equals("/upload.jsp"))
        		{
        			
        		}
        	}
        	if(user_role.equals("FNSTer"))
        	{
        		
        	}
        }
		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException
	{
		// TODO Auto-generated method stub
	}

}
